#!/bin/bash
# 2018.06.09

######################################################################
#
#  Copyright (c) 2015 arakasi72 (https://github.com/arakasi72)
#
#  Modified by Aniverse
#
#  --> Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
#
######################################################################

PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/bin:/sbin

osname=$(lsb_release -si)
relno=$(lsb_release -sr | cut -d. -f1)
sourcedir='http://rtorrent.net/downloads/'

xmlrpc_url='https://svn.code.sf.net/p/xmlrpc-c/code/stable/'
xmlrpcloc='svn://svn.code.sf.net/p/xmlrpc-c/code/stable/'

xmlrpc_url_alt='https://github.com/mirror/xmlrpc-c'
xmlrpcloc_alt='https://github.com/mirror/xmlrpc-c/trunk/stable'

rtdevrel=1
enable_ipv6=1

# Check for options
while getopts ":i" optname; do
    case $optname in
        "i" ) enable_ipv6=0 ;;
    esac
done

[[ $enable_ipv6 == 0 ]] && ipv6opt=--enable-ipv6 && ipv6show="(with IPv6 support)"

if [ $relno = 9 ] || [ $(lsb_release -sr) = 17.10 ] || [ $relno = 18 ]; then
  rtdevrel=0
fi

# if [ $rtdevrel = 0 ]; then
#   sourcedir='https://github.com/rakshasa/'
# fi

listsize=6
duoversion=''
passed_rtvers=$1
[[ $passed_rtvers == "-i" ]] && passed_rtvers=$2
[ -z $logfile ] && logfile="/dev/null"

# get a list of the most recent releases
rt_releases=$(wget -q -O - $sourcedir | grep -o "rtorrent-[0-9]\{1,2\}\.[0-9]\{1,2\}\.[0-9]\{1,2\}\.tar.gz" | sort -uV | tail -$listsize | grep -o "[0-9]\{1,2\}\.[0-9]\{1,2\}\.[0-9]\{1,2\}")

# extract the release numbers from the list of most recent releases
for rtversion in $rt_releases
  do
    libversion=$(echo $rtversion | awk -F. -v OFS=. '{$2='$(( $(echo $rtversion | cut -d. -f2) + 4 ))'; print }')
    duoversion="$duoversion $libversion/$rtversion"
  done

# get the most recent release version number
latest_duo=$(echo $duoversion | rev | cut -d' ' -f1 | rev)
latest_libvers=$(echo $latest_duo | cut -d/ -f1)
latest_rtvers=$(echo $latest_duo | cut -d/ -f2)

# checks if an application is installed
installed() {
hash $1 2>/dev/null
}

#function to check if a web site is reachable
check_url() {
  if [[ `wget -S --spider $1  2>&1 | grep 'HTTP/1.1 200 OK'` ]]; then return 0; else return 1; fi
}

# function to create a list of versions to install
list_version() {
local verschoice
while [ -z "$verschoice" ]
do
  echo "Please select libtorrent/rtorrent version."
  select verschoice in ${duoversion[*]}
  do
    libvers=$(echo $verschoice | cut -d/ -f1)
    rtvers=$(echo $verschoice | cut -d/ -f2)
    break
  done
done
}

ask_user(){
local answer
while true
  do
    read answer
    case $answer in [Yy]* ) return 0 ;;
                    [Nn]* ) return 1 ;;
                        * ) echo "Enter y or n";;
    esac
  done
}

if [ "$(id -u)" != "0" ]; then
  echo "Must be run as root, or with sudo"
  exit 1
fi

# Check the libtorrent/rtorrent and xmlrpc sites are available
xmlrpc_repo=0
rt_repo=0
echo
echo "Checking File Repositories"

echo -n "xmlrpc-c: "; check_url $xmlrpc_url && echo "OK" ||  xmlrpc_repo=1

if [[ $xmlrpc_repo = 1 ]]; then
  xmlrpc_url=$xmlrpc_url_alt
  xmlrpcloc=$xmlrpcloc_alt
  check_url $xmlrpc_url && { echo "OK";  xmlrpc_repo=0; } || { echo "FAIL"; xmlrpc_repo=1; }
fi

echo -n "rtorrent: "; check_url $sourcedir && echo "OK" || { echo "FAIL"; rt_repo=1; }

if [ $rt_repo = 1 ]; then
  echo "The libtorrent/rtorrent file repositoy is offline"
  echo "Try again later, you can check if the following address is online in your browser:"
  echo $sourcedir
  exit 1
fi

if [ $xmlrpc_repo = 1 ]; then
  if [ -d /usr/include/xmlrpc-c ]; then
    echo "xmlrpc repository is off line, but looks like it has already beed installed."
    echo "We will try and install rtorrent"
  else
    echo "The xmlrpc-c file repositoy is offline"
    echo "Try again later, you can check if the following address is online in your browser:"
    echo $xmlrpcloc
    exit 1
  fi
fi

if [ -z $passed_rtvers ]; then

    if ( installed rtorrent ); then
        rt_current=$(rtorrent -h | grep -om 1 "[0-9]\{1,2\}\.[0-9]\{1,2\}\.[0-9]\{1,2\}")
        echo "rtorrent $rt_current detected"
    else
        echo "rtorrent NOT installed"
        echo "This script is for upgrading/downgrading only, not for initial install"
        exit 1
    fi

    list_version

    echo "Install libtorrent-$libvers and rtorrent-$rtvers"
    echo "Your rtorrent config, torrents, and data will not be lost or changed"
    echo -n "Continue? "
    if ! ask_user; then
        exit
    fi

else

    if [ "$passed_rtvers" = "latest" ] || [ $(wget -q -O - $sourcedir | grep -o "rtorrent-$passed_rtvers.tar.gz" | wc -l) -eq 0 ]; then
        if [ "$passed_rtvers" != "latest" ]; then
            echo "Could not find $passed_rtvers so using latest version instead"
        fi
        libvers=$latest_libvers
        rtvers=$latest_rtvers
    else
        libvers=$(echo $passed_rtvers | awk -F. -v OFS=. '{$2='$(( $(echo $rtversion | cut -d. -f2) + 4 ))'; print }')
        rtvers=$passed_rtvers
    fi

fi

if [ -z "$home" ]; then
  home=$HOME
fi

echo
echo "Installing libtorrent-$libvers/rtorrent-$rtvers $ipv6show"

cd $home

if [ -d source ]; then
  cd source
else
  mkdir source && cd source
fi
rm -rf xmlrpc libtorrent* rtorrent*

echo "Fetching source files"



if [[ $enable_ipv6 == 0 ]] && [[ $rtvers == 0.9.6 ]]; then
    git clone https://github.com/rakshasa/libtorrent libtorrent-$libvers >> $logfile 2>&1
    cd libtorrent-$libvers
    git checkout 9eb9ba2 >> $logfile 2>&1
    cd ..
    git clone https://github.com/rakshasa/rtorrent rtorrent-$rtvers >> $logfile 2>&1
    cd rtorrent-$rtvers
    git checkout b088c1c >> $logfile 2>&1
    cd ..
elif [[ $rtvers == 0.9.7 ]]; then
    git clone https://github.com/rakshasa/libtorrent libtorrent-0.13.7 >> $logfile 2>&1
    cd libtorrent-0.13.7
    git checkout v0.13.7 >> $logfile 2>&1
    git config --global user.email "you@example.com" >> $logfile 2>&1
    git config --global user.name "Your Name" >> $logfile 2>&1
    git cherry-pick 7b29b6b >> $logfile 2>&1
    cd ..
    git clone --depth=1 -b v0.9.7 --single-branch https://github.com/rakshasa/rtorrent rtorrent-$rtvers >> $logfile 2>&1
elif [[ $enable_ipv6 == 0 ]] && [[ ! $rtvers == 0.9.6 ]]; then
    wget -qO- https://github.com/Aniverse/BitTorrentClientCollection/raw/master/rTorrent-IPv6/libtorrent-$libvers.tar.gz | tar xz
    wget -qO- https://github.com/Aniverse/BitTorrentClientCollection/raw/master/rTorrent-IPv6/rtorrent-$rtvers.tar.gz    | tar xz
else
    curl -s http://rtorrent.net/downloads/libtorrent-$libvers.tar.gz | tar xz
    curl -s http://rtorrent.net/downloads/rtorrent-$rtvers.tar.gz    | tar xz
fi




if [ $xmlrpc_repo = 0 ]; then
  svn co $xmlrpcloc xmlrpc  >> $logfile 2>&1

  cd xmlrpc
  echo "Installing xmlrpc" | tee -a $logfile
  ./configure --prefix=/usr --disable-cplusplus >> $logfile 2>&1
  make -j$(nproc) >> $logfile 2>&1
  make -s install >> $logfile 2>&1
  cd ..
fi

cd libtorrent-$libvers
echo "Installing libtorrent-rakshasa" | tee -a $logfile
./autogen.sh >> $logfile 2>&1
if [ $osname = "Raspbian" ]; then
  ./configure --prefix=/usr --disable-instrumentation $ipv6opt >> $logfile 2>&1
else
  ./configure --prefix=/usr $ipv6opt >> $logfile 2>&1
fi
make -j$(nproc) >> $logfile 2>&1
make -s install >> $logfile 2>&1

cd ../rtorrent-$rtvers
echo "Installing rtorrent" | tee -a $logfile
./autogen.sh >> $logfile 2>&1
./configure --prefix=/usr --with-xmlrpc-c $ipv6opt >> $logfile 2>&1
make -j$(nproc) >> $logfile 2>&1
make -s install >> $logfile 2>&1
ldconfig >> $logfile 2>&1

 echo "$libvers/$rtvers installed"
 echo

if [ -d /var/www/rutorrent/conf/users ]; then
  cd /var/www/rutorrent/conf/users
  user_list=*
  for user in $user_list; do
    if [ ! "$user" = '*' ]; then
      echo "Restarting rtorrent for $user"
      su $user -c 'rt restart >> /dev/null 2>&1'
    fi
  done
  echo
fi
